<?php

namespace app\common\service\api;
use app\common\service\member\SettingService as MemberSetting;
use app\common\service\setting\Exception;
use app\common\service\system\SettingService;
use thirdsdk\Http;

/**
 * 阿里接口
 */
class AlibabaSdkService
{
    /**
     * 身份证识别
     * @Author: 易军辉
     * @DateTime:2024-08-01 16:19
     * @param $side 身份证正反面：face/back
     * @param $file_path  图片地址
     * @return array
     * @throws \think\Exception
     */
    public static function idCardIdentify($side='face',$file_path=''){
        $result = array(
            'code'=>400,
            'msg'=>'系统繁忙，请稍后再试',
            'data'=>null,
        );
        if($side!='face' && $side!='back'){
            $result['code'] = 200;
            $result['msg'] = '无需识别';
            return  $result;
        }
        $memberSetting = MemberSetting::info('is_idcard_recognize');
        if($memberSetting['is_idcard_recognize'] == 1){
            //获取身份证信息
            if(!$file_path){
                $result['msg'] = "请重新上传身份证".($side=='face'?'正面':'反面');
                return $result;
            }
            //初始化配置信息
            $sysSetting = SettingService::info('ali_idcard_url,ali_idcard_code');
            if(!isset($sysSetting['ali_idcard_url']) || !isset($sysSetting['ali_idcard_code']) || !$sysSetting['ali_idcard_url'] || !$sysSetting['ali_idcard_code']){
                $result['msg'] = "请配置阿里云身份证识别接口参数";
                return $result;
            }
            try {
                $http = new Http('aliIdCard');
                $body = [
                    "image" =>self::img_base64($file_path),
                    "configure"=>json_encode([
                        'side'=>$side,
                        'quality_info'=>true
                    ])
                ];
                $headers = [
                    "Authorization:APPCODE " . $sysSetting['ali_idcard_code'],
                    "Content-Type:application/json; charset=UTF-8"
                ];

                $card_front_res  = $http->post($sysSetting['ali_idcard_url'],$body,$headers);
                if($card_front_res){
                    $result['code'] = 200;
                    $result['msg'] = '识别成功';
                    $result['data'] =$card_front_res;
                    return $result;
                }
            } catch (\Exception $e) {
                exception($e->getMessage());
            }
        }else{
            $result['code'] = 401;
            $result['msg'] = '未开启身份证文字识别';
        }
        return $result;
    }

    /**
     * 根据图片地址转64位
     * @Author: 易军辉
     * @DateTime:2024-08-01 15:54
     * @param $path
     * @return mixed|string
     * @throws \think\Exception
     */
    private static function img_base64($path){
        //对path进行判断，如果是本地文件就二进制读取并base64编码，如果是url,则返回
        $img_data="";
        if (substr($path,0,strlen("http")) === "http"){
            $img_data = $path;
        }else{
            if($fp = fopen($path, "rb", 0)) {
                $binary = fread($fp, filesize($path)); // 文件读取
                fclose($fp);
                $img_data = base64_encode($binary); // 转码
            }else{
                exception('身份证未读取到');
            }
        }
        return $img_data;
    }


    /**
     * 三要素接口识别
     * @Author: 易军辉
     * @DateTime:2024-08-01 21:22
     * @param $name 姓名
     * @param $idCard 身份证号
     * @param $mobile 手机号
     * @return array
     */
    public static function threeElements($name='',$idCard='',$mobile=''){
        $result = array(
            'code'=>400,
            'msg'=>'系统繁忙，请稍后再试',
            'data'=>null,
        );
        $memberSetting = MemberSetting::info('is_idcard_auth');
        if($memberSetting['is_idcard_auth'] == 1){
            //获取身份证信息
            if(!$name || !$idCard || !$mobile){
                $result['msg'] = "请完善姓名、身份证号、手机号等信息";
                return $result;
            }
            //初始化配置信息
            $sysSetting = SettingService::info('ali_3ys_url,ali_3ys_code');
            if(!isset($sysSetting['ali_3ys_url']) || !isset($sysSetting['ali_3ys_code']) || !$sysSetting['ali_3ys_url'] || !$sysSetting['ali_3ys_code']){
                $result['msg'] = "请配置阿里云三要素接口参数";
                return $result;
            }

            try {
                $http = new Http('ali3ys');
                $body = [
                    "biz_content"=>json_encode([
                        'name'=>$name,
                        'idCard'=>$idCard,
                        'mobile'=>$mobile,
                    ])
                ];
                $headers = [
                    "Authorization:APPCODE " . $sysSetting['ali_3ys_code'],
                    "Content-Type:application/x-www-form-urlencoded; charset=UTF-8"
                ];

                $card_front_res  = $http->post($sysSetting['ali_3ys_url'],$body,$headers);
                if($card_front_res){
                    $result['code'] = 200;
                    $result['msg'] = '识别成功';
                    $result['data'] =$card_front_res;
                    return $result;
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                if($msg=='不一致'){
                    $msg ="您的姓名、身份证、手机号三者不一致，请检查";
                }
                exception($msg);
            }
        }else{
            $result['code'] = 401;
            $result['msg'] = '未开启身份证认证三要素';
        }
        return $result;
    }

    /**
     * 银行卡识别
     * @Author: 易军辉
     * @DateTime:2024-08-05 16:19
     * @param $file_path  图片地址
     * @return array
     * @throws \think\Exception
     */
    public static function bankCardOcr($file_path=''){
        $result = array(
            'code'=>400,
            'msg'=>'系统繁忙，请稍后再试',
            'data'=>null,
        );
        $memberSetting = MemberSetting::info('is_bank_card_recognize');
        if($memberSetting['is_bank_card_recognize'] == 1){
            //获取身份证信息
            if(!$file_path){
                $result['msg'] = "请重新上传银行卡";
                return $result;
            }
            //初始化配置信息
            $sysSetting = SettingService::info('ali_bank_url,ali_bank_code');
            if(!isset($sysSetting['ali_bank_url']) || !isset($sysSetting['ali_bank_code']) || !$sysSetting['ali_bank_url'] || !$sysSetting['ali_bank_code']){
                $result['msg'] = "请配置阿里云银行卡识别接口参数";
                return $result;
            }
            try {
                $http = new Http('bankCard');
                $body = [
                    "image" =>self::img_base64($file_path),
                    "configure"=>json_encode([
                        'card_type'=>true
                    ])
                ];
                $headers = [
                    "Authorization:APPCODE " . $sysSetting['ali_bank_code'],
                    "Content-Type:application/json; charset=UTF-8"
                ];

                $post_res  = $http->post($sysSetting['ali_bank_url'],$body,$headers);
                if($post_res){
                    $result['code'] = 200;
                    $result['msg'] = '识别成功';
                    $result['data'] =$post_res;
                    return $result;
                }
            } catch (\Exception $e) {
                exception($e->getMessage());
            }
        }else{
            $result['code'] = 401;
            $result['msg'] = '未开启银行卡文字识别';
        }
        return $result;
    }
}
